package com.joinway.framework.admin.core.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.joinway.framework.admin.bean.domain.Page;
import com.joinway.framework.admin.core.bean.PageMetaData;
import com.joinway.framework.admin.core.service.file.FileConstants;
import com.joinway.framework.admin.core.service.file.FileManager;
import com.joinway.framework.admin.core.service.file.IFileRepository;
import com.joinway.framework.admin.core.utils.VelocityProcessor;
import com.joinway.framework.bean.domain.constant.DomainConstants;
import com.joinway.framework.bean.type.Server;
import com.joinway.framework.core.repository.SystemConfigRepository;
import com.joinway.framework.data.jdbc.repository.TableRepository;
import com.joinway.framework.extension.utils.DataUtils;

@Deprecated
@Service
public class NoticeService {

	private final static Logger log = LoggerFactory.getLogger(NoticeService.class);
	
	final static String NOTICE_TEMPLATE = "template/notice.jsp";
	
	final static String HTML_KEY = "html";

	@Autowired SystemConfigRepository systemRepository;
	
	@Autowired TableRepository repository;
	
	@Autowired VelocityProcessor processor;
	
	public void acknowledge(String page, int userId) throws Exception {
		String fileName = page + systemRepository.getStringValue(SystemConfigRepository.PageSuffix);
		
		boolean exists = FileManager.exists(FileConstants.NoticeFile.Path, fileName, Server.App);
		
		if(!exists){
			int pageId = DataUtils.convertToInt(StringUtils.split(page, "-")[0]);
			Page p = repository.findById(Page.class, pageId);
			
			if(p != null){
				synchronized(this){
					exists = FileManager.exists(FileConstants.NoticeFile.Path, fileName, Server.App);
					if(!exists){
						log.debug("file {} doesn't exist, create it", fileName);
						
						NoticePageRepository noticePageRepository = new NoticePageRepository(p.getText());
						
						FileManager.save(FileConstants.NoticeFile.Path, fileName, Server.App, noticePageRepository);
					}
				}
			}
		}
		
		int pageId = DataUtils.convertToInt(StringUtils.split(page, "-")[0]);
		
		if(userId > 0){
			String sql = "update user_notice_log set status = :status, accept_time = :acceptTime where user_id = :userId and page_id = :pageId";
			
			Map<String, Object> params = new HashMap<>();
			params.put("userId", userId);
			params.put("pageId", pageId);
			params.put("status", DomainConstants.YES);
			params.put("acceptTime", new Date());
			
			repository.update(params, sql);
		}
		
		String sql = "update push_log set read_count = read_count + 1 where page_id = ?";
		
		repository.update(sql, pageId);
	}
	
	public PageMetaData createPage(String html, String createBy) throws Exception {
		PageMetaData data = new PageMetaData();
		
		Page page = new Page();
		page.setText(html);
		page.setCreateBy(createBy);
		repository.save(page);
		
		int pageId = page.getId();
		
		String fileName = FileManager.getAvailableName(String.valueOf(pageId)) + systemRepository.getStringValue(SystemConfigRepository.PageSuffix);

		NoticePageRepository noticePageRepository = new NoticePageRepository(page.getText());
		String url = FileManager.save(FileConstants.NoticeFile.Path, fileName, Server.App, noticePageRepository);
		
		data.setPageId(pageId);
		data.setUrl(url);
		
		return data;
	}

	class NoticePageRepository implements IFileRepository<String>{
		String text;
		
		NoticePageRepository(String text){
			this.text = text;
		}
		
		@Override
		public void save(String file, String context) throws Exception {
			Map<String, Object> model = new HashMap<>();
			model.put(HTML_KEY, text);
			processor.process(NOTICE_TEMPLATE, model, file);
		}
	}
}


